<!--

This install script may be run once the php files are in place,
such as after unpacking a tarball, or checking out the project from subversion

-->

<?php
require_once 'masterpage.php';


$db_cfg_file = "db_access.php";
$db_access = false;
$db_file = false;
$db_table = false;
$db_installed = false;

$db_hostname = null;
$db_database = null;
$old_db_username = null;
$old_db_password = null;
$db_username = null;
$db_password = null;
$db_backup_directory = null;

function check_if_true($bool_var)
{
    $green_check = '<span style="color:green;">&#x2713;</span>';
    $red_ex = '<span style="color:red;">&#x2717;</span>';

    if($bool_var)
    {
        return $green_check;
    }
    return $red_ex;
}

function create_db_file($fname, $hname, $dbname, $uname, $upass,
                        $admin_uname, $admin_upass, $bkdir)
{
    $file = fopen($fname, 'w');
    if ($file == false)
    {
        return false;
    }
    fprintf($file, "<?php\n\n/**\n*\n".
            "* This small php file is designed to allow database access.\n".
            "* set your mySQL parameters here\n".
            "* @author - Joshua A Clayton\n".
            "* @version - 1.0\n*/\n\n".
            "\$db_hostname = '%s';\n".
            "\$db_database = '%s';\n".
            "\$db_username = '%s';\n".
            "\$db_password = '%s';\n".
            "\$db_admin_username = '%s';\n".
            "\$db_admin_password = '%s';\n".
            "\$db_salt     = 'a7^Doe8*02as3f*29&5nma7';\n".
            "\$db_directory = '%s';\n".
            "\n?>\n",
            $hname, $dbname, $uname, $upass,
            $admin_uname, $admin_upass, $bkdir);
}

if (file_exists($db_cfg_file) && include $db_cfg_file)
{
    $db_file = true;
    if (isset($db_hostname) && isset($db_username) && isset($db_password))
    {
        $link = mysql_connect($db_hostname, $db_username,$db_password);
        if ($link)
        {
            $db_access = true;
            $result = mysql_select_db($db_database);
            if ($result)
            {
                $db_table = true;
                //This is good. Access confirmed
            }
            else
            {
                $db_err = mysql_error($link);
                //Find out whether its is access denied or table not found
            }
        }
        else
        {
            $db_err = mysql_error();
            //troubleshoot db access failure
        }
    }

    $installed = true;

    if ($link == false)
    {
        //prompt for 
    }
}
else
{
    if (isset($_POST["db_hostname"]))
    {
        //TODO! Add some checks to make sure the params are valid before writing
        create_db_file($db_cfg_file,
                       $_POST["db_hostname"],
                       $_POST["db_port"],
                       $_POST["db_name"],
                       $_POST["db_user"],
                       $_POST["db_passwd"],
                       $_POST["admin_user"],
                       $_POST["admin_passwd"],
                       $_POST["db_directory"]);
    }
    $installed = false;

}
masterpage("Setup");
echo "Database error: " . $db_err;

if ($installed == true)
{
    echo "<h2 style =\"color: red\">Caution: Making incorrect changes to the Setup page may cause damage to the system. Be careful, and make a backup before making any changes</h2>";
}
//if (!isset($db_hostname ))
//    $db_hostname = 'localhost';
?>

    <div id="Installer">
      <form method="post" action="setup.php">
    <h1>Step by step installation</h1>
    <ol>
      <li>URL is set for the server where the MySQL database resides
      <?php  echo check_if_true($db_file); ?> <br />
        Database Hostname or URL: <input type="text"
                                   name="db_hostname"
                                   value=<?php echo $db_hostname;?>
                                   /><br />
        Database Port Number:<input type="text"
                             name="db_port"
                             value="Use default"
                             /><br />
      </li>
      <li>MySQL database is installed and running
      </li>
      <li>User name and password of the User account for the database
      <?php echo check_if_true($db_access) ?> <br />
        Database User Name: <input type="text"
                             name="db_user"
                             value=<?php echo $db_username;?>
                             /><br />
        Database Password: <input type="password"
                            name="db_passwd"
                            value=<?php echo $db_password;?>
                            /><br />
      </li>
      <li>Name of the database where system is or will be installed
      <?php echo check_if_true($db_table) ?> <br />
        Database Name: <input type="text"
                        name="db_name"
                        value=<?php echo $db_database;?>
                        /><br />
      </li>
      <li>Database tables installed</li>
      <li>Administrator Account for the online system is present</li>
      <li>username and password of the Administrator account for the database</li>
      <li>Directory or URL where system backups will be stored</li>
    </ol>
        <input type="submit" value="Go" />
      </form>
    </div>  
<?php
endmasterpage();
?>
